import { Memento } from 'vscode';
import * as Api from './api';
import * as Token from '../../tokens';

const logger = (message: string) => console.log(message);

const client = Api.getClient('https://gitee.com/api/v5/', logger);

export const createRepository = async (memento: Memento, {
  name,
  description = '',
  isPrivate,
}: {
  name: string;
  description?: string;
    isPrivate: boolean;
}) => {
  const { token } = Token.getToken(memento)['gitee'];
  if (token) {
    const response = await client.createRepository({
      name,
      description,
      access_token: token,
      private: isPrivate,
    });

    console.log(response);
    return response;
  }
  else {
    console.log('缺少 access token');
  }

};

export const createGist = async (memento: Memento, {
  file,
  content,
  description = '',
  isPublic = false,
}: {
  file: string;
  content: string;
  description: string;
  isPublic: boolean;
}) => {
  const { token } = Token.getToken(memento)['gitee'];

  if (token) {
    const response = await client.createGist({
      access_token: token,
      description,
      files: {
        [file]: {
          content,
        }
      },
      public: isPublic,
    });

    console.log(response);
    return response;
  }
  else {
    console.log('缺少 access token');
  }
};